# coding=utf-8
from datetime import datetime, timedelta

from klog import client
from klog.cli import models


def main():
    # endpoint 可选择 klog-cn-beijing.ksyun.com 或者 klog.api.ksyun.com
    # 如果使用 klog-cn-beijing.ksyun.com 则会收取读取流量的费用
    # 但是如果使用 klog.api.ksyun.com 则不能使用 push 函数
    c = client.Client(endpoint="endpoint", access_key="access_key", secret_key="secret_key")

    req = models.CreateProjectRequest("project_name")
    res = c.CreateProject(req)
    # 创建工程操作,没有返回值,可以使用res.get_request_id()和res.get_status()查看request_id和操作状态
    print(res.get_request_id(), res.get_status())

    req = models.ListProjectsRequest(0, 10)
    res = c.ListProjects(req)
    for project in res.Projects:
        print(project.ProjectName)

    # 如果是多日志池的查询，则log_pool_name使用逗号分隔开
    req = models.GetLogsRequest(project_name="project_name", log_pool_name="pool1,pool2,pool3",
                                start_time=(datetime.now() - timedelta(minutes=15)).timestamp() * 1000,
                                end_time=datetime.now().timestamp() * 1000)
    res = c.GetLogs(req)
    print(res.serialize(), res.get_data())
    for l in res.Logs:
        print(l.timestamp)

    pass


if __name__ == '__main__':
    main()
